package com.my.helper;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.my.helper.fragment.FragmentBeiWang;
import com.my.helper.fragment.FragmentMain;
import com.my.helper.fragment.FragmentUser;
import com.my.helper.fragment.FragmentZiXun;
import com.my.helper.util.CacheActivity;
import com.my.helper.util.MyDataBaseHelper;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup mainRadioGroup;
    private RadioButton rbMain;
    private RadioButton rbZiXun;
    private RadioButton rbBeiWang;
    private RadioButton rbMessage;
    private FragmentManager fragmentManager;
    private FragmentMain fragmentMain;
    private FragmentZiXun fragmentZiXun;
    private FragmentBeiWang fragmentBeiWang;
    private FragmentUser fragmentMessage;
    //    private MyDataBaseHelper dbHelper;
    private MyDataBaseHelper mSql;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CacheActivity.addActivity(this);
        fragmentManager = getSupportFragmentManager();
        crateBWDataBase();
        initView();
        initListener();
        setTabSelection(0);
    }

    /**
     * 创建备忘录数据库
     */
    private void crateBWDataBase() {
        //MyDatabaseHelper的参数分别为:上下文, 数据库名为Helper.db, CursorFactory为null, 版本号为1
        mSql = new MyDataBaseHelper(this, "Helper.db", null, 1);
        /**
         * getWritableDatabase:表示可以创建或打开一个现有的数据库(如果数据库已经存在则直接打开,
         *  否则创建一个新的数据库), 并返回一个对数据库进行读写操作的对象
         *  getReadableDatabase:同上, 但是如果出现不可写入, 比如磁盘满了, 此方法返回的对象以只读的方式打开
         *  而getWritableDatabase则会抛出异常.
         *
         */
        mSql.getWritableDatabase();
    }

    private void initView() {
        mainRadioGroup = findViewById(R.id.rg_main);
        rbMain = findViewById(R.id.rb_main);
        rbZiXun = findViewById(R.id.rb_zi_xun);
        rbBeiWang = findViewById(R.id.rb_bei_wang);
        rbMessage = findViewById(R.id.rb_user);
    }

    private void initListener() {
        mainRadioGroup.setOnCheckedChangeListener(this);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_main: // 首页
                setTabSelection(0);
                break;
            case R.id.rb_zi_xun: // 资讯
                setTabSelection(1);
                break;
            case R.id.rb_bei_wang: // 备忘
                setTabSelection(2);
                break;
            case R.id.rb_user: // 账户
                setTabSelection(3);
                break;
        }
    }

    private void setTabSelection(int i) {
        // 开启一个Fragment 事物
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragmetn,防止多个Fragment显示在界面上
        hideFragments(transaction);
        switch (i) {
            case 0:
                if (fragmentMain == null) {
                    fragmentMain = new FragmentMain();
                    if (!fragmentMain.isAdded()) {
                        transaction.add(R.id.content_fragment, fragmentMain);
                    }
                } else {
                    transaction.show(fragmentMain);
                }
                break;
            case 1:
                if (fragmentZiXun == null) {
                    fragmentZiXun = new FragmentZiXun();
                    if (!fragmentZiXun.isAdded()) {
                        transaction.add(R.id.content_fragment, fragmentZiXun);
                    }
                } else {
                    transaction.show(fragmentZiXun);
                }
                break;
            case 2:
                if (fragmentBeiWang == null) {
                    fragmentBeiWang = new FragmentBeiWang();
                    if (!fragmentBeiWang.isAdded()) {
                        transaction.add(R.id.content_fragment, fragmentBeiWang);
                    }
                } else {
                    transaction.show(fragmentBeiWang);
                }
                break;
            case 3:
                if (fragmentMessage == null) {
                    fragmentMessage = new FragmentUser();
                    if (!fragmentMessage.isAdded()) {
                        transaction.add(R.id.content_fragment, fragmentMessage);
                    }
                } else {
                    transaction.show(fragmentMessage);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 先隐藏掉所有的Fragment
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (fragmentMain != null) {
            if (!fragmentMain.isHidden()) { // 不是隐藏的
                transaction.hide(fragmentMain);// 隐藏
            }
        }
        if (fragmentZiXun != null) {
            if (!fragmentZiXun.isHidden()) { // 不是隐藏的
                transaction.hide(fragmentZiXun);// 隐藏
            }
        }
        if (fragmentBeiWang != null) {
            if (!fragmentBeiWang.isHidden()) { // 不是隐藏的
                transaction.hide(fragmentBeiWang);// 隐藏
            }
        }
        if (fragmentMessage != null) {
            if (!fragmentMessage.isHidden()) { // 不是隐藏
                transaction.hide(fragmentMessage);// 隐藏
            }
        }
    }
}
